package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbTemplateMapper;
import com.guigu.qingcheng.pojo.TbTemplate;
import com.guigu.qingcheng.service.TbTemplateService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
*
* @author Jiang
* @date 2020/07/25
*/
@Service
public class TbTemplateServiceImpl implements TbTemplateService {
    @Autowired(required = false)
    private TbTemplateMapper tbTemplateMapper;


    @Override
    public List<TbTemplate> queryAll() {
        List<TbTemplate> tbTemplate = tbTemplateMapper.selectAll();
            return tbTemplate;
    }

    @Override
    public PageUtil<TbTemplate> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbTemplate> pages = (Page<TbTemplate>) tbTemplateMapper.selectAll();
        PageUtil<TbTemplate> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbTemplate> pageQueryByAll(Integer pageNum, Integer pageSize, TbTemplate tbTemplate) {

        Example example=new Example(TbTemplate.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        if(!StringUtils.isEmpty(tbTemplate.getId())){
            criteria.andEqualTo("id",tbTemplate.getId());
        }
        if(!StringUtils.isEmpty(tbTemplate.getName())){
           criteria.andLike("name","%"+tbTemplate.getName()+"%");
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<TbTemplate> pages = (Page<TbTemplate>) tbTemplateMapper.selectByExample(example);
        PageUtil<TbTemplate> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbTemplate(TbTemplate tbTemplate) {
        int insert = tbTemplateMapper.insert(tbTemplate);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbTemplate(TbTemplate tbTemplate) {
        int i = tbTemplateMapper.updateByPrimaryKeySelective(tbTemplate);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbTemplateMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbTemplate findById(Integer id) {
        TbTemplate tbTemplate = tbTemplateMapper.selectByPrimaryKey(id);
        return tbTemplate;
    }
}
